/**
 ******************************************************************************
 * @file    
 * @author 
 * @Version 
 * @Date    
 * @brief 
 *******************************************************************************  
 **/
#include "zb32l03x_hal.h"
#include "log.h"
#include "bsp_powerctrl.h"
#include "FreeRTOS.h"
#include "queue.h"
#include "Ringbuff.h"
#include "ui_lcd.h"
static uint8_t LoopShortFlag;

/*
*********************************************************************************************************
*	功能说明: 初始化
*	形    参: 
*	返 回 值: 
* 说    明：
*********************************************************************************************************
*/

void app_powerctrl_init(void)
{
  bsp_powerctrl_init();
  LoopShortFlag = 0;
}

uint8_t app_powerctrl_getbatterlevel(void)
{
  static uint32_t batval;
  static uint8_t sample = 0;
  static char level = 4;
  uint16_t temp;
  if (BSP_POWERCTRL_EXPOWER() == 0)
  {
    batval = batval + bsp_powerctrl_getbattervalue();
    if (sample >= 16)
    {
      batval = batval / sample;
      if (batval < 1638) // 小于2V，电量0
      {
        level = 1;
      }
      else if (batval < 1884) // 2.3V,电量1
      {
        level = 2;
      }
      else if (batval < 2130) // 2.6V,电量2
      {
        level = 3;
      }
      else
      {
        level = 4;
      }
      sample = 0;
      batval = 0;
    }
    else
    {
      sample++;
    }
  }
  else
  {
    level = 0;
    sample = 0;
    batval = 0;
  }
  return level;
}

void app_powerctrl_systempower(uint8_t onoff)
{
  if (onoff == 1)
  {
    BSP_POWERCTRL_SYSPOWERON();
  }
  else
  {
    BSP_POWERCTRL_SYSPOWEROFF();
  }
}

void app_powerctrl_short(uint8_t flag)
{
  if (flag)
  {
    BSP_POWERCTRL_POWEROFF();
    LoopShortFlag = 1;
    bsp_powerctrl_openvc(HAL_VC_INTERRUPT_TRIGGER_RISE);
    msg("short\r\n");
    ui_lcd_postkeyval(&uitcb, UI_KEYVAL_SHORT_UI);
    
  }
  else
  {
    LoopShortFlag = 0;
    bsp_powerctrl_openvc(HAL_VC_INTERRUPT_TRIGGER_FALL);
    msg("short em\r\n");
    ui_lcd_postkeyval(&uitcb, UI_KEYVAL_SHORT_EM_UI);
  }
}

/*
*********************************************************************************************************
*	功能说明: 判断是否有短路
*	形    参: 
*	返 回 值: 
* 说    明：
*********************************************************************************************************
*/
uint8_t  app_powerctrl_isshortdet(void)
{
   return LoopShortFlag;  
}

/*
*********************************************************************************************************
*	功能说明: 判断当前电源状态
*	形    参: 
*	返 回 值: 
* 说    明：
*********************************************************************************************************
*/

bool app_powerctrl_isexpower(void)
{
   if (BSP_POWERCTRL_EXPOWER() == 0)
   {
    return false;
   }
   else
   {
    return true;
   }
}

/*
*********************************************************************************************************
*	功能说明: 扫描电源状态
*	形    参: 
*	返 回 值: 
* 说    明：
*********************************************************************************************************
*/
void app_powerctrl_process(void)
{
   static uint8_t loopstate_short = 0;
   static uint8_t debugcnt = 0;
   uint8_t curloopstate;
   curloopstate = app_powerctrl_isshortdet();
   if (loopstate_short != curloopstate)
   {
    debugcnt++;
    if (debugcnt > 10)
    {
      loopstate_short = curloopstate;
      if (loopstate_short)
      {
        msg("short\r\n");
        ui_lcd_postkeyval(&uitcb, UI_KEYVAL_SHORT_UI);
      }
      else
      {
        msg("short em\r\n");
        ui_lcd_postkeyval(&uitcb, UI_KEYVAL_SHORT_EM_UI);
      }
    }
   }
   else
   {
    debugcnt = 0;
   }
}
